﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace test14 {
    public class gamemanerge : MonoBehaviour
    {
        public Text time;//时间文本
        private float timer = 60;//时间
        public GameObject enemy;//敌人
        private playercontroller player;//玩家
        //失败 成功文本
        public GameObject text_defeat;
        public GameObject text_suscess;
        private enemy enemyScript;//敌人脚本
      
        // Start is called before the first frame update
        void Start()
        {
            time.text = "倒计时：" + timer;
            StartCoroutine(reduce());
            player = GameObject.Find("player").GetComponent<playercontroller>();
        }
        /// <summary>
        /// 检测是否成功
        /// </summary>
        private void checksucess()
        {
            if (timer <= 0 && player.count > 0)
            {
                text_defeat.SetActive(true);
                Time.timeScale = 0;
            }
            else if (timer > 0 && player.count <= 0)
            {
                text_suscess.SetActive(true);
                Time.timeScale = 0;
            }

        }
        
        // Update is called once per frame
        void Update()
        {
            checksucess();
           checkRay();
        }
        /// <summary>
        /// 射线检测是否碰到敌人
        /// </summary>
        private void checkRay()
        {
            if (Input.GetMouseButtonDown(1))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider.CompareTag("enemy"))
                    {
                        enemyScript = hit.collider.GetComponent<enemy>();                   
                        enemyScript.isclick = true;
                    }
                }
            }
        }
        /// <summary>
        /// 协程使时间减少
        /// </summary>
        /// <returns></returns>
        IEnumerator reduce()
        {
            while (timer >= 0)
            {
                yield return new WaitForSeconds(1);
                timer -= 1f;
                time.text = "倒计时：" + timer;               
            }

        }
        
    }
}
